﻿using System;
using System.Windows.Forms;
using DocumentManager.Abstractions.Factories;
using DocumentManager.Abstractions.Interfaces.Helpers;
using DocumentManager.Enums;

namespace DocumentManager.Forms
{
	public partial class frmLogin : Form
	{
		#region Constants

		private const string MOTHER_USER_NAME = "Mam";

		#endregion

		#region Variables

		private readonly IPasswordGenerator _passwordGenerator;
		private readonly FormBaseFactory _formFactory;

		#endregion

		#region Constructors

		public frmLogin(IPasswordGenerator passwordGenerator, FormBaseFactory formFactory)
		{
			if (passwordGenerator == null)
				throw new ArgumentNullException(string.Format("password Generator"));
			_passwordGenerator = passwordGenerator;

			if (formFactory == null)
				throw new ArgumentNullException(string.Format("form Factory"));
			_formFactory = formFactory;

			InitializeComponent();
		}

		#endregion

		#region Form Actions

		private void btnLogin_Click(object sender, EventArgs e)
		{
			var dateTime = DateTime.Now;
			var validPassword = _passwordGenerator.GeneratePasswordByDate(dateTime);

			if (txtLogin.Text.Equals(MOTHER_USER_NAME, StringComparison.OrdinalIgnoreCase)
			    && txtPassword.Text.Equals(validPassword))
			{
				this.Visible = false;

				var form = _formFactory.CreateForm(FormType.DocumentManager);
				if (form != null)
					form.ShowDialog();

				this.Close();
				this.DialogResult = DialogResult.Cancel;
			}
			else
				MessageBox.Show(string.Format("Неверные логин и пароль!{0}Пожалуйста, повторите попытку входа в систему.", Environment.NewLine));
		}

		#endregion
	}
}
